package com.example.front.main.the_order;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.front.MyApplication;
import com.example.front.R;
import com.example.front.main.home.Boardgame;
import com.example.front.main.home.BoardgameActivity;
import com.example.front.main.home.BoardgameIntroductionActivity;
import com.example.front.main.home.PsGameActivity;
import com.example.front.main.shop.SelectProduct;
import com.example.front.utils.MyAdapter;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class OrderProductActivity extends AppCompatActivity {
    private String URL;
    private MyApplication myApplication;
    private TextView room_textView;
    private TextView cost_textView;
    private ListView product_listView;
    private MyAdapter<SelectProduct> productMyAdapter = null;
    private List<SelectProduct> productList = null;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_product);
        myApplication = (MyApplication) getApplication();
        room_textView = findViewById(R.id.room_textView);
        cost_textView = findViewById(R.id.cost_textView);
        product_listView = findViewById(R.id.product_listView);
        URL = myApplication.getURL();
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        int orderID = bundle.getInt("orderID", 0);
        String room = bundle.getString("room", "");
        double cost = bundle.getDouble("cost", 0);
        room_textView.setText(room + "房");
        cost_textView.setText("合计  ￥" + cost);
        OkHttpUtils.post()
                .url(URL + "/order/getOrderProductList")
                .addParams("orderID", String.valueOf(orderID))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        String json = response;
                        Log.i("success", "获取成功！");
                        productList = new ArrayList<SelectProduct>();
                        try {
                            JSONArray jsonArray = new JSONArray(json);
                            for (int i = 0; i < jsonArray.length(); i++){
                                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                                SelectProduct product = new SelectProduct();
                                product.setId(jsonObject.getInt("id"));
                                product.setName(jsonObject.getString("name"));
                                product.setPrice(jsonObject.getDouble("price"));
                                product.setUnit(jsonObject.getString("unit"));
                                product.setType(jsonObject.getString("type"));
                                product.setNumber(jsonObject.getInt("count"));
                                product.setPhoto(jsonObject.getString("photo"));
                                productList.add(product);
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        productMyAdapter = new MyAdapter<SelectProduct>((ArrayList) productList, R.layout.order_product_list_item){
                            @Override
                            public void bindView(ViewHolder holder, SelectProduct obj){
                                imageView = holder.getView(R.id.product_image);
                                Glide.with(OrderProductActivity.this)
                                        .load(obj.getPhoto())
                                        .error(R.drawable.error)
                                        .crossFade()
                                        .into(imageView);
                                holder.setText(R.id.name_textView, obj.getName() + "");
                                holder.setText(R.id.type_textView, " " + obj.getType() + " " + obj.getPrice() + "/" + obj.getUnit());
                                holder.setText(R.id.product_number_textView, "x " + obj.getNumber());
                                holder.setText(R.id.product_total_textView, "￥" + obj.getNumber() * obj.getPrice());
                            }
                        };
                        product_listView.setAdapter(productMyAdapter);
                    }
                });
    }
}
